/*
 * @lc app=leetcode id=111 lang=cpp
 *
 * [111] Minimum Depth of Binary Tree
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {

        int h = 0;
        if (!root) {
            return h;
        }
        h = minDepth(root, h);
        return h;
    }

    int minDepth(TreeNode *root, int h) {
        if (!root) {
            return INT_MAX;
        }

        if (!root->left && !root->right) {
            return h+1;
        }

        int lh = minDepth(root->left, h);
        int rh = minDepth(root->right, h);
        return (lh < rh ? lh : rh) + 1;
    }
};

